package com.czl.module_storehouse.activity.search;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.czl.base.config.AppConstants;
import com.czl.base.data.bean.tengyun.SortBean;
import com.czl.module_base.activity.BaseRecyclerViewActivity;
import com.czl.module_base.bean.ListBean;
import com.czl.module_base.mvp.presenter.InjectPresenter;
import com.czl.module_base.mvp.view.SimpleView;
import com.czl.module_service.event.SortBeanEvent;
import com.czl.module_storehouse.R;
import com.czl.module_storehouse.activity.baseInfo.GoodsBaseInfoActivity;
import com.czl.module_storehouse.adapter.SortSearchListAdapter;
import com.czl.module_storehouse.databinding.HeaderSortSearchListBinding;
import com.czl.module_storehouse.mvp.presenter.StockPresenter;
import com.jeremyliao.liveeventbus.LiveEventBus;

import java.util.ArrayList;

/**
 * @author xubin
 */
@Route(path = AppConstants.Router.Service.F_SERVICE_SEARCH_LIST)
public class SortSearchListActivity extends BaseRecyclerViewActivity<HeaderSortSearchListBinding> implements SimpleView {

    @InjectPresenter
    StockPresenter mStockPresenter;
    private SortSearchListAdapter mAdapter;

    @Override
    protected HeaderSortSearchListBinding getChildBinding(LayoutInflater layoutInflater, FrameLayout containerTop) {
        return HeaderSortSearchListBinding.inflate(layoutInflater, containerTop, true);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        mToolBinding.toolbarContentTitle.setText("物品查询");
        mAdapter = new SortSearchListAdapter(R.layout.item_sort_search_list, new ArrayList<>());
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            SortBean item = mAdapter.getItem(position);
            LiveEventBus.get(SortBeanEvent.class).postOrderly(new SortBeanEvent(item));
            startActivity(new Intent(getContext(), GoodsBaseInfoActivity.class));
        });
        mChildBinding.textSearch.setOnClickListener(v -> search());
    }

    protected void initListener() {
        mChildBinding.etSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (TextUtils.isEmpty(s.toString())) {
                    binding.recyclerView.setVisibility(View.GONE);
                }
            }
        });
    }

    @Override
    protected void loadData() {
        super.loadData();
        loadData(true);
    }

    @Override
    protected void loadMore() {
        super.loadMore();
        loadData(false);
    }

    private void loadData(boolean refresh) {
        mStockPresenter.getStorehouseStockInfo(refresh, mChildBinding.etSearch.getText().toString());
    }

    private void search() {
        String text = mChildBinding.etSearch.getText().toString();
        if (TextUtils.isEmpty(text)) {
            return;
        }
        mStockPresenter.getStorehouseStockInfo(true, text);
    }

    @Override
    public <T> void showData(T t) {
        super.showData(t);
        handleResponseData(mAdapter, mStockPresenter.getPageNo(), (ListBean<SortBean>) t);
    }
}
